import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:hkzf_flutter/scoped_model/auth_model.dart';
import 'package:hkzf_flutter/utils/dio_http.dart';
import 'package:hkzf_flutter/utils/scoped_model_helper.dart';
// 房源图片上传处理
Future<String> uploadImages(List<File> files,BuildContext context) async{
  if(files.isEmpty) return Future.value("");
  // 等待所有 MultipartFile 创建完成
  var images = await Future.wait(files.map((file) async {
    return await MultipartFile.fromFile(file.path, filename: "picture.png");
  }));
  // 构建上传文件的表单对象
  FormData formdata = FormData.fromMap({
      "file": images,
  });
  // print('formdata:${formdata.files}');
  // 发起上传请求
  var path = '/houses/image'; 
  // 获取状态存储中的token信息
  var token = ScopedModelHelper.getModel<AuthModel>(context).token;
  var res = await DioHttp.of(context).postFormData(path,formdata,token);
  var data = json.decode(res.toString())['body'];
  String imgList = List<String>.from(data).join('|');
  print('上传结果：${res}');
  return Future.value(imgList);
}